﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DemoTileDesktop
{
    using Microsoft.Research.Kinect;
    using Microsoft.Research.Kinect.Nui;
    using KINECTUtility;
    using System.Windows.Media.Animation;

    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        private Runtime nuiRuntime;
        private AreaChecker myAreaChecker;

        private double unitWidth = 64;
        private double unitHeight = 64;

        public MainWindow()
        {
            InitializeComponent();

            double windowWidth = 1280;
            double windowHeight = 768;
            myAreaChecker = new AreaChecker(windowWidth, windowHeight, unitWidth, unitHeight);
            myAreaChecker.HoldTime = TimeSpan.FromMilliseconds(3000);

//            myAreaChecker.RegistCheckJointID(JointID.HandLeft);
            myAreaChecker.RegistCheckJointID(JointID.HandRight);

            myAreaChecker.AreaMoved += new AreaChecker.AreaCheckerUpdateEventHandler(myAreaChecker_AreaMoved);
            myAreaChecker.AreaHold += new AreaChecker.AreaCheckerHoldEventHandler(myAreaChecker_AreaHold);
            myAreaChecker.AreaUnhold += new AreaChecker.AreaCheckerHoldEventHandler(myAreaChecker_AreaUnhold);

            nuiRuntime = new Runtime();
            nuiRuntime.Initialize(RuntimeOptions.UseColor | RuntimeOptions.UseSkeletalTracking);
            nuiRuntime.SkeletonFrameReady += new EventHandler<SkeletonFrameReadyEventArgs>(nuiRuntime_SkeletonFrameReady);

            kinectJointApp = this.myDesktop.KinectJointAppl;
        }

        private KinectJointApp kinectJointApp;
        private double lastDirection = 0;
        private double lastTXProperty = 0;
        private bool isLastHeld = false;
        void myAreaChecker_AreaMoved(AreaCheckerUpdateEventArgs e)
        {
            kinectJointApp.SetAreaMotion(e.CurrentIndexHorizontal,e.CurrentIndexVertical, "Move to");
            if (!isLastHeld)
            {
                return;
            }
            isLastHeld = false;
//            if (lastDirection != 0)
  //          {
    //            lastDirection = System.Math.Sign(e.MoveHorizontalCells);
      //          return;
        //    }
            if (System.Math.Abs(e.MoveHorizontalCells) > 0)
            {
                double delta = unitWidth * 4;
                double toValue = 0;
                if (e.MoveToLeft)
                {
                    toValue = lastTXProperty - delta;
                    if (toValue < -944)
                    {
                        toValue = -944;
                    }
                }
                else if (e.MoveToRight)
                {
                    toValue = lastTXProperty + delta;
                    if (toValue > 0)
                    {
                        toValue = 0;
                    }
                }
                if (lastTXProperty != toValue)
                {
                    TranslateTransform transform = new TranslateTransform();
                    myDesktop.RenderTransform = transform;
                    transform.BeginAnimation(
                        TranslateTransform.XProperty,
                        new DoubleAnimation(lastTXProperty, toValue, new Duration(TimeSpan.FromMilliseconds(1000))));
                    lastTXProperty = toValue;
                }

            }
            lastDirection = System.Math.Sign(e.MoveHorizontalCells);
        }

        void myAreaChecker_AreaHold(AreaCheckerHoldEventArgs e)
        {
            kinectJointApp.SetAreaMotion(e.HoldIndexHorizontal, e.HoldIndexVertical, "Hold");
            isLastHeld = true;
        }

        void myAreaChecker_AreaUnhold(AreaCheckerHoldEventArgs e)
        {
  //          throw new NotImplementedException();
        }

        void nuiRuntime_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            SkeletonFrame frame = e.SkeletonFrame;
            foreach (var skelton in frame.Skeletons)
            {
                if (skelton.TrackingState == SkeletonTrackingState.Tracked)
                {
                    myAreaChecker.BeginCheck();
                    float hlx = skelton.Joints[JointID.HandLeft].Position.X;
                    float hly = skelton.Joints[JointID.HandLeft].Position.Y;

                    float hrx = skelton.Joints[JointID.HandRight].Position.X;
                    float hry = skelton.Joints[JointID.HandRight].Position.Y;

                //    myAreaChecker.IsIncludePoint(hlx, hly, JointID.HandLeft);
                    myAreaChecker.IsIncludePoint(hrx, hry, JointID.HandRight);

                    myAreaChecker.EndCheck();
                    break;
                }
            }
        }
    }
}
